package org.dtrd.modules.suggest.controller;


import org.dtrd.config.system.SystemState;
import org.dtrd.modules.service.service.impl.DtrdEntRdServiceServiceImpl;
import org.dtrd.modules.suggest.entity.bean.PeriodFoodScore;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.impl.FoodRecordServiceImpl;
import org.dtrd.modules.suggest.service.impl.DtrdEntRdSigninRecordSuggestServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.suggest.entity.bean.SigninRecordSuggestDetail;
import org.dtrd.modules.suggest.service.IDtrdEntRdSigninRecordSuggestService;
import org.dtrd.common.api.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import org.dtrd.base.BaseController;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 打卡记录建议表 前端控制器
 * </p>
 * @author sun
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/signin/suggest/")
@Api(tags = "打卡记录评分建议")
public class DtrdEntRdSigninRecordSuggestController extends BaseController {

    @Autowired
    private IDtrdEntRdSigninRecordSuggestService signinRecordSuggestService;

    @Autowired
    private DtrdEntRdSigninRecordSuggestServiceImpl recordSuggestService;

    @ApiOperation("新增或修改打卡记录建议表")
    @PostMapping("/save")
    @AutoLog(value="新增或修改打卡记录建议表", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateSigninRecordSuggest(@RequestBody @Validated SigninRecordSuggestDetail dto){
        boolean result = signinRecordSuggestService.saveOrUpdateSigninRecordSuggest(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除打卡记录建议表")
    @DeleteMapping("/delete")
    @AutoLog(value="删除打卡记录建议表", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeSigninRecordSuggest(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = signinRecordSuggestService.removeSigninRecordSuggest(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation(value = "获取饮食得分曲线")
    @PostMapping("/get")
    public Result<?> getFoodTimePeriod(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        String startDate = null;
        String endDate = null;
        String[] timePeriod = handleTimePeriod(request);
        if (timePeriod != null) {
            startDate = timePeriod[0];
            endDate = timePeriod[1];
        }
        if (startDate == null || endDate == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        Integer patientId = request.getPatientId();
        PeriodFoodScore record = recordSuggestService.getFoodScoreForCharts(patientId, startDate, endDate);
        return Result.OK(record);
    }
}
